// 添加渲染进程调试代码
const { ipcRenderer, contextBridge } = require('electron');
const path = require('path');
const fs = require('fs');

// 日志记录函数
function logToFile(message) {
  try {
    const logPath = path.join(__dirname, '../logs/render-error.log');
    const logContent = `[${new Date().toISOString()}] ${message}\n`;
    fs.appendFileSync(logPath, logContent);
  } catch (err) {
    console.error('无法写入日志:', err);
  }
}

// 监听渲染进程错误
window.addEventListener('error', (event) => {
  logToFile(`错误: ${event.message}\n堆栈: ${event.error?.stack || '无堆栈信息'}`);
});

window.addEventListener('unhandledrejection', (event) => {
  logToFile(`未处理的Promise拒绝: ${event.reason}`);
});

// 导出 API 到渲染进程
contextBridge.exposeInMainWorld('electron', {
  ipcRenderer: {
    send: (channel, ...args) => {
      ipcRenderer.send(channel, ...args);
    },
    on: (channel, func) => {
      ipcRenderer.on(channel, (event, ...args) => func(...args));
    },
    invoke: async (channel, ...args) => {
      return await ipcRenderer.invoke(channel, ...args);
    }
  },
  log: (message) => {
    logToFile(message);
  }
}); 